package br.praticas.projetoacademia.views;
import br.praticas.projetoacademia.dao.AlunoDao;
import br.praticas.projetoacademia.dao.ContatoDao;
import br.praticas.projetoacademia.dao.EnderecoDao;
import br.praticas.projetoacademia.dao.PlanoDao;
import br.praticas.projetoacademia.models.Aluno;
import br.praticas.projetoacademia.models.Contato;
import br.praticas.projetoacademia.models.Endereco;
import br.praticas.projetoacademia.util.Check;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.apache.logging.log4j.LogManager;

/**
 * @author lucia
 * @since 16/11/2014
 * @see classe EnderecoDao
 * @see classe ContatoDao
 * @see classe AlunoDao
 * @see classe aluno
 */
public class AlunoView extends javax.swing.JDialog {

    Aluno aluno = new Aluno();
    Contato contatos = new Contato();
    Endereco enderecos = new Endereco();
    PlanoDao planoDao = new PlanoDao();
    AlunoDao daoAluno = new AlunoDao();
    EnderecoDao daoEndereco = new EnderecoDao();
    ContatoDao daoContato = new ContatoDao();
    List<Aluno> listaModelAluno = new ArrayList<>();
    List<Endereco> listaModelEndereco = new ArrayList<>();
    List<Contato> listaModelContato = new ArrayList<>();
    String tipoCadastro;
    private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger(AlunoView.class);
    
    AlunoView(){
        initComponents();
        setLocationRelativeTo(null);
        prencherTabela();
    }
    
    /**
     * Metodo que lista todos os alunos em uma tabela, com seus respectivos dados.
     */

    private void prencherTabela(){
        String colunas[] = {"Matricula","Nome","CPF","Status", "Cidade", 
            "Bairro","Cep", "Rua", "Número","Uf",
            "Celular", "Fixo", "Email","Data","Valor"};
        listaModelAluno = daoAluno.listarAluno();
        String dados[][] = new String[listaModelAluno.size()][16];
        //CARREGA OS DADOS DA LISTA NA TABELA
        int cont = listaModelAluno.size();
        for (int i = 0; i < cont; i++) {
            dados[i][0] = listaModelAluno.get(i).getMatricula();
            dados[i][1] = listaModelAluno.get(i).getNome();
            dados[i][2] = listaModelAluno.get(i).getCpf();
            dados[i][3] = listaModelAluno.get(i).getStatus();
            dados[i][4] = listaModelAluno.get(i).getEndereco().getCidade();
            dados[i][5] = listaModelAluno.get(i).getEndereco().getBairro();
            dados[i][6] = listaModelAluno.get(i).getEndereco().getCep();
            dados[i][7] = listaModelAluno.get(i).getEndereco().getRua();
            dados[i][8] = listaModelAluno.get(i).getEndereco().getNumero();
            dados[i][9] = listaModelAluno.get(i).getEndereco().getEstado();
            dados[i][10] = listaModelAluno.get(i).getContato().getCelular();
            dados[i][11] = listaModelAluno.get(i).getContato().getTelefoneFixo();
            dados[i][12] = listaModelAluno.get(i).getContato().getEmail();
            dados[i][13] = listaModelAluno.get(i).getDataDeVencimento();
            dados[i][14] = listaModelAluno.get(i).getPlano();
            dados[i][15] = listaModelAluno.get(i).getValor();         
        }
        TableModel model = new DefaultTableModel(dados, colunas);
        tabela.setModel(model);
    }
    /**
     * Metodo que limpa os dados da interface aluno
     */
    private void limpar() {
        
        Cpf.setText("");
        Nome.setText("");
        bairro.setText("");
        cidade.setText("");
        cep.setText("");
        uf.setText("");
        celular.setText("");
        telefone.setText("");
        email.setText("");
        rua.setText("");
        numero.setText("");
        Pesquisa.setText("");
        plano.setText("");
        status.setText("");
        plano.setText("");
        valor.setText("");
    }
    
    /**
     * Metodo que desabilita os campos e botoes da tela aluno
     */

    protected void desabilitarCampos() {
       
        Nome.setEditable(false);
        Cpf.setEditable(false);
        status.setEditable(false);
        cidade.setEditable(false);
        rua.setEditable(false);
        bairro.setEditable(false);
        numero.setEditable(false);
        cep.setEditable(false);
        uf.setEditable(false);
        celular.setEditable(false);
        telefone.setEditable(false);
        email.setEditable(false);
        plano.setEditable(false);
        valor.setEditable(false);
        Pesquisa.setEditable(false);
        Salvar.setEnabled(false);
    }

    /**
     * Metodo que habilita os dados da interface aluno
     */
    private void habilitarCampos() {
        
        
        Nome.setEditable(true);
        Cpf.setEditable(true);
        bairro.setEditable(true);
        cidade.setEditable(true);
        cep.setEditable(true);
        uf.setEditable(true);
        celular.setEditable(true);
        telefone.setEditable(true);
        email.setEditable(true);
        Pesquisa.setEditable(true);
        rua.setEditable(true);
        numero.setEditable(true);
        status.setEditable(true);
        plano.setEditable(true);
        valor.setEditable(true);
        Salvar.setEnabled(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Novo = new javax.swing.JButton();
        Excluir = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        Salvar = new javax.swing.JButton();
        Cpf = new javax.swing.JTextField();
        Alterar = new javax.swing.JButton();
        Pesquisa = new javax.swing.JTextField();
        Nome = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        bairro = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        celular = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        uf = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        telefone = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        numero = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cep = new javax.swing.JTextField();
        cidade = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        rua = new javax.swing.JTextField();
        btPesquisaCliente1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        status = new javax.swing.JTextField();
        plano = new javax.swing.JTextField();
        valor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Aluno");

        Novo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/Add.png"))); // NOI18N
        Novo.setText("Novo");
        Novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NovoActionPerformed(evt);
            }
        });

        Excluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/Delete.png"))); // NOI18N
        Excluir.setText("Excluir");
        Excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExcluirActionPerformed(evt);
            }
        });

        Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/No.png"))); // NOI18N
        Cancelar.setText("Cancelar");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        tabela.setAutoCreateRowSorter(true);
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Matricula", "Nome", "Cpf", "Status", "Cidade", "Bairro", "Cep", "Rua", "Numero", "Uf", "Celular", "Fixo", "Email", "Plano", "Data", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tabela.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tabela);

        jLabel10.setText("Cpf:");

        Salvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/Save.png"))); // NOI18N
        Salvar.setText("Salvar");
        Salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalvarActionPerformed(evt);
            }
        });

        Cpf.setEditable(false);

        Alterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/Modify.png"))); // NOI18N
        Alterar.setText("Alterar");
        Alterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AlterarActionPerformed(evt);
            }
        });

        Nome.setEditable(false);

        jLabel8.setText("Nome:");

        jLabel11.setText("Status:");

        jLabel3.setText("Bairro:");

        jLabel4.setText("Cidade:");

        jLabel6.setText("Cep:");

        bairro.setEditable(false);
        bairro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bairroActionPerformed(evt);
            }
        });

        jLabel2.setText("Endereço:");

        celular.setEditable(false);
        celular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                celularActionPerformed(evt);
            }
        });

        jLabel5.setText("Uf:");

        uf.setEditable(false);
        uf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ufActionPerformed(evt);
            }
        });

        email.setEditable(false);
        email.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailActionPerformed(evt);
            }
        });

        jLabel12.setText("Celelular:");

        jLabel13.setText("Telefone Fixo:");

        telefone.setEditable(false);
        telefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                telefoneActionPerformed(evt);
            }
        });

        jLabel14.setText("Email:");

        numero.setEditable(false);
        numero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numeroActionPerformed(evt);
            }
        });

        jLabel7.setText("Contato:");

        cep.setEditable(false);

        cidade.setEditable(false);

        jLabel15.setText("Rua:");

        jLabel16.setText("Número");

        rua.setEditable(false);
        rua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ruaActionPerformed(evt);
            }
        });

        btPesquisaCliente1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/praticas/projetoacademia/imagens/View.png"))); // NOI18N
        btPesquisaCliente1.setText("Pesquisar");
        btPesquisaCliente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaCliente1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Dados Pessoais:");

        jLabel19.setText("Dados referente ao Plano Mensal:");

        jLabel18.setText("Valor:");

        jLabel20.setText("Tipo:");

        status.setEditable(false);

        plano.setEditable(false);
        plano.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                planoActionPerformed(evt);
            }
        });

        valor.setEditable(false);
        valor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cidade, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rua, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bairro, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cep, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(uf, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(Cpf, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(celular, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(telefone, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(Pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPesquisaCliente1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel20)
                            .addComponent(jLabel18))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(plano, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 730, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel19)))
                .addGap(0, 10, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(Cancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Excluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Alterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Novo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Salvar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1)
                .addGap(1, 1, 1)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(2, 2, 2)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cidade, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(26, 26, 26))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(bairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(rua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel3)))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(uf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabel7)
                .addGap(11, 11, 11)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(celular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(plano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisaCliente1))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Alterar)
                    .addComponent(Cancelar)
                    .addComponent(Novo)
                    .addComponent(Excluir)
                    .addComponent(Salvar))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void NovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NovoActionPerformed
         tipoCadastro = "novo";
        habilitarCampos();
    
        
    }//GEN-LAST:event_NovoActionPerformed

    /**
     * Metodo que excluir um aluno do banco
     */
    private void ExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExcluirActionPerformed
        DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
        int index = tabela.getSelectedRow();
        aluno = daoAluno.buscarAluno(index);
        if (Check.checkNull(aluno)) {
            JOptionPane.showConfirmDialog(null, "Voce tem certeza que deseja excluir o aluno selecionado?", "?", JOptionPane.YES_NO_OPTION);
            daoAluno.remove(aluno, contatos, enderecos);
            LOGGER.info("deletado");
            JOptionPane.showMessageDialog(null, "aluno excluido com suscesso!");
            prencherTabela();
        } else {
            JOptionPane.showMessageDialog(this, "Erro ao Excluir!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_ExcluirActionPerformed

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
       dispose();
    }//GEN-LAST:event_CancelarActionPerformed

    /**
     * Metodo que salva um aluno no banco, onde prenche seus respectinos dados
     * esse metodo tambem altera os dados dos alunos.
     * @see metodo AlterarActionPerformed
     */
    private void SalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalvarActionPerformed

       
        aluno.setNome(this.Nome.getText());
        aluno.setCpf(this.Cpf.getText());
        aluno.setStatus(this.plano.getText());
        enderecos.setCidade(this.cidade.getText());
        enderecos.setBairro(this.bairro.getText());
        enderecos.setCep(this.cep.getText());
        enderecos.setRua(this.rua.getText());
        enderecos.setNumero(this.numero.getText());
        enderecos.setEstado(this.uf.getText());
        contatos.setCelular(this.celular.getText());
        contatos.setTelefoneFixo(this.telefone.getText());
        contatos.setEmail(this.email.getText());
        aluno.setPlano(this.plano.getText());
        aluno.setValor(this.valor.getText());
        aluno.setEndereco(enderecos);
        aluno.setContato(contatos);
             Date matricula= new Date(System.currentTimeMillis());
             Calendar vencimento = Calendar.getInstance();
             vencimento.setTime(matricula);
             vencimento.add(Calendar.MONTH, 1);
        aluno.setDataDeVencimento(vencimento.getTime());
        
        if (tipoCadastro.equals("novo")) {
            //daoAluno.add(aluno, contatos, enderecos);
            aluno.setMatricula(aluno.getId() + "" + aluno.getNome().length());
            daoAluno.add(aluno, contatos, enderecos);
            LOGGER.info("Aluno Salvo no banco com sucesso!");
            JOptionPane.showMessageDialog(null, "aluno cadastrado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
        } else if (tipoCadastro.equals("alterar")) {
            daoAluno.update(aluno, contatos, enderecos);
            LOGGER.info("Aluno editado  com sucesso!");
            JOptionPane.showMessageDialog(null, "aluno editado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
        }
        limpar();
        this.desabilitarCampos();
         
     
    }//GEN-LAST:event_SalvarActionPerformed

    /**
     * Metodo que altera  os dados de um aluno
     */
    private void AlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AlterarActionPerformed
        DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
            int index = tabela.getSelectedRow();
            aluno = daoAluno.buscarAluno(index);
           
            this.Nome.setText(aluno.getNome());
            this.Cpf.setText(aluno.getCpf());
            this.plano.setText(aluno.getStatus());
            this.cidade.setText(enderecos.getCidade());
            this.bairro.setText(enderecos.getBairro());
            this.cep.setText(enderecos.getCep());
            this.rua.setText(enderecos.getRua());
            this.numero.setText(enderecos.getNumero());
            this.uf.setText(enderecos.getEstado());
            this.celular.setText(contatos.getCelular());
            this.telefone.setText(contatos.getTelefoneFixo());
            this.email.setText(contatos.getEmail());
            this.plano.setText(aluno.getPlano());
            this.valor.setText(aluno.getValor());
            tipoCadastro = "alterar";
            habilitarCampos();
        
    }//GEN-LAST:event_AlterarActionPerformed

    private void bairroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bairroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bairroActionPerformed

    private void celularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_celularActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_celularActionPerformed

    private void ufActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ufActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ufActionPerformed

    private void emailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_emailActionPerformed

    private void telefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telefoneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_telefoneActionPerformed

    private void numeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numeroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_numeroActionPerformed

    private void ruaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ruaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ruaActionPerformed

    /**
     * Metodo que busca por apenas um aluno, essa busca e feita pelo nome.
     */
    private void btPesquisaCliente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaCliente1ActionPerformed
        DefaultTableModel tabela = (DefaultTableModel) this.tabela.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tabela);
        this.tabela.setRowSorter(sorter);
        String text = Pesquisa.getText();
        sorter.setRowFilter(RowFilter.regexFilter(text, 1));
    }//GEN-LAST:event_btPesquisaCliente1ActionPerformed

    private void planoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_planoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_planoActionPerformed

    private void valorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new AlunoView().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(AlunoView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Alterar;
    private javax.swing.JButton Cancelar;
    protected javax.swing.JTextField Cpf;
    private javax.swing.JButton Excluir;
    protected javax.swing.JTextField Nome;
    private javax.swing.JButton Novo;
    protected javax.swing.JTextField Pesquisa;
    private javax.swing.JButton Salvar;
    protected javax.swing.JTextField bairro;
    private javax.swing.JButton btPesquisaCliente1;
    protected javax.swing.JTextField celular;
    protected javax.swing.JTextField cep;
    protected javax.swing.JTextField cidade;
    protected javax.swing.JTextField email;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    protected javax.swing.JTextField numero;
    protected javax.swing.JTextField plano;
    protected javax.swing.JTextField rua;
    protected javax.swing.JTextField status;
    private javax.swing.JTable tabela;
    protected javax.swing.JTextField telefone;
    protected javax.swing.JTextField uf;
    protected javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
}
